<!--
 * @Author: Huangxianlong
 * @Date: 2023-09-05 15:48:38
 * @LastEditors: Huangxianlong
 * @LastEditTime: 2023-09-05 16:23:31
 * @FilePath: \vue3-element-admin\src\views\login\index.vue
 * @Description: 
-->
<script setup lang="ts">
import router from "@/router";
import LangSelect from "@/components/LangSelect/index.vue";
import SvgIcon from "@/components/SvgIcon/index.vue";

// 状态管理依赖
import { useUserStore } from "@/store/modules/user";

// API依赖
import { LocationQuery, LocationQueryValue, useRoute } from "vue-router";
import { getCaptchaApi } from "@/api/auth";
import { LoginData } from "@/api/auth/types";

const userStore = useUserStore();
const route = useRoute();

/**
 * 按钮loading
 */
const loading = ref(false);
/**
 * 是否大写锁定
 */
const isCapslock = ref(false);
/**
 * 密码是否可见
 */
const passwordVisible = ref(false);
/**
 * 验证码图片Base64字符串
 */
const captchaBase64 = ref();

/**
 * 登录表单引用
 */
const loginFormRef = ref(ElForm);

const loginData = ref<LoginData>({
  username: "admin",
  password: "123456",
});

const loginRules = {
  username: [{ required: true, trigger: "blur" }],
  password: [{ required: true, trigger: "blur", validator: passwordValidator }],
  verifyCode: [{ required: true, trigger: "blur" }],
};

/**
 * 密码校验器
 */
function passwordValidator(rule: any, value: any, callback: any) {
  if (value.length < 6) {
    callback(new Error("The password can not be less than 6 digits"));
  } else {
    callback();
  }
}

/**
 * 检查输入大小写状态
 */
function checkCapslock(e: any) {
  const { key } = e;
  isCapslock.value = key && key.length === 1 && key >= "A" && key <= "Z";
}

/**
 * 获取验证码
 */
function getCaptcha() {
  getCaptchaApi().then(({ data }) => {
    loginData.value.captchaKey = data.captchaKey;
    captchaBase64.value = data.captchaBase64;
  });
}
/**
 * 登录
 */
function handleLogin() {
  loginFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      userStore
        .login(loginData.value)
        .then(() => {
          const query: LocationQuery = route.query;

          const redirect = (query.redirect as LocationQueryValue) ?? "/";

          const otherQueryParams = Object.keys(query).reduce(
            (acc: any, cur: string) => {
              if (cur !== "redirect") {
                acc[cur] = query[cur];
              }
              return acc;
            },
            {}
          );

          router.push({ path: redirect, query: otherQueryParams });
        })
        .catch(() => {
          // 验证失败，重新生成验证码
          getCaptcha();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}

onMounted(() => {
  getCaptcha();
});
</script>
<template>
  <div class="login-main">
    <div class="login-left">
      <div class="left-header">
        <img src="../../assets/logo.png" alt="" />
        <span>智慧农业监测系统</span>
      </div>
      <div class="left-content">
        <div style="text-align: center">
          <img src="./img/login-left-bg.svg" alt="" />
          <h1>欢迎使用智慧农业监测系统</h1>
        </div>
      </div>
    </div>
    <div class="login-right">
      <div class="right-header">
        <img src="../../assets/logo.png" alt="" />
        <span>智慧农业监测系统</span>
      </div>
      <div class="login-from">
        <el-form
          ref="loginFormRef"
          :model="loginData"
          :rules="loginRules"
          size="small"
          class="login-el-form"
        >
          <el-form-item prop="username">
            <div class="p-2">
              <svg-icon icon-class="user" size="18px" />
            </div>
            <el-input
              class="flex-1"
              ref="username"
              v-model="loginData.username"
              placeholder="用户名"
              clearable
              name="username"
            />
          </el-form-item>

          <el-form-item prop="password">
            <span class="p-2">
              <svg-icon icon-class="password" size="18px" />
            </span>
            <el-input
              class="flex-1"
              v-model="loginData.password"
              placeholder="密码"
              clearable
              show-password
              name="password"
              @keyup="checkCapslock"
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item prop="verifyCode">
            <span class="p-2">
              <svg-icon icon-class="verify_code" size="18px" />
            </span>
            <el-input
              v-model="loginData.verifyCode"
              auto-complete="off"
              placeholder="验证码"
              class="w-[60%]"
              @keyup.enter="handleLogin"
            />

            <div class="captcha">
              <img :src="captchaBase64" @click="getCaptcha" />
            </div>
          </el-form-item>
          <el-form-item style="border: none">
            <el-button
              class="w-full"
              size="large"
              type="primary"
              :loading="loading"
              @click="handleLogin"
            >
              {{ $t("login.login") }}
            </el-button>
          </el-form-item>
          <el-form-item style="border: none">
            <el-divider>第三方登录</el-divider>
            <el-row
              style="width: 100%; text-align: center; margin-bottom: 10px"
            >
              <el-col :span="6"
                ><svg-icon icon-class="wechat" size="30px" class="icon-hover"
              /></el-col>
              <el-col :span="6"
                ><svg-icon icon-class="alipay" size="30px" class="icon-hover"
              /></el-col>
              <el-col :span="6"
                ><svg-icon icon-class="qq" size="30px" class="icon-hover"
              /></el-col>
              <el-col :span="6"
                ><svg-icon icon-class="blog" size="30px" class="icon-hover"
              /></el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login-main {
  display: flex;
  height: 100vh;
}

.login-left {
  flex: 1;
  padding: 30px;
  background: rgba(10, 49, 73);

  .left-header {
    display: flex;
    align-items: center;
    color: #fff;
    line-height: 48px;

    img {
      margin-right: 10px;
      height: 50px;
    }

    span {
      font-size: 30px;
      font-weight: bold;
    }
  }

  .left-content {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    height: calc(100% - 100px);

    img {
      height: 300px;
    }
  }
}

.login-right {
  flex: 1;
  padding: 30px;
  background: #fff;

  .login-from {
    height: calc(100% - 30px);
    display: flex;
    align-items: center;
    justify-content: center;

    .login-el-form {
      width: 360px;
    }

    .captcha {
      position: absolute;
      top: 0;
      right: 5px;

      img {
        width: 120px;
        height: 40px;
        cursor: pointer;
      }
    }

    :deep(.svg-icon) {
      vertical-align: -0.3rem;
    }
  }

  .right-header {
    display: none;
    align-items: center;
    color: #fff;
    line-height: 48px;

    img {
      margin-right: 10px;
      height: 50px;
    }

    span {
      font-size: 30px;
      font-weight: bold;
      white-space: nowrap;
    }
  }

  :deep(.el-input__suffix) {
    padding-right: 5px;
  }
}

.icon-hover {
  color: #5d5d5d;
  cursor: pointer;
}

.icon-hover:hover {
  color: #409eff !important;
}

.el-form-item {
  border: 1px solid #ccc;
  background: rgb(255, 255, 255);
  border-radius: 5px;
}

.el-input {
  background: transparent;

  // 子组件 scoped 无效，使用 :deep
  :deep(.el-input__wrapper) {
    padding: 0;
    background: transparent;
    box-shadow: none !important;

    .el-input__inner {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      color: #2a3451;
      caret-color: #2a3451;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px transparent inset !important;
        -webkit-text-fill-color: #2a3451 !important;
      }

      // 设置输入框自动填充的延迟属性
      &:-webkit-autofill,
      &:-webkit-autofill:hover,
      &:-webkit-autofill:focus,
      &:-webkit-autofill:active {
        -webkit-transition-delay: 99999s;
        -webkit-transition: color 99999s ease-out,
          background-color 99999s ease-out;
      }
    }
  }
}

@media screen and (max-width: 1180px) {
}

@media screen and (max-width: 968px) {
  .login-left {
    display: none;
  }

  .login-right {
    background: rgba(10, 49, 73);
  }

  .right-header {
    display: flex !important;
  }
}

@media screen and (max-width: 375px) {
  .login-el-form {
    width: 300px !important;
  }
}
</style>
